package com.dingup.provider.controller.lesson;

import com.dingup.api.Util.RedisUtils;
import com.dingup.api.adapter.student.StudentCourseInformationAdapter;
import com.dingup.api.base.HttpResContent;
import com.dingup.api.bean.page.PageData;
import com.dingup.api.bean.requestBean.lesson.LessonInfoBean;
import com.dingup.api.bean.requestBean.lesson.LessonPageBean;
import com.dingup.api.bean.requestBean.subject.ExamSubjectBean;
import com.dingup.api.enums.*;
import com.dingup.api.model.lesson.LessonInfo;
import com.dingup.api.model.lesson.LessonSubjectInfo;
import com.dingup.provider.service.classInfo.ClassInfoService;
import com.dingup.provider.service.lesson.LessonService;
import com.suyeer.basic.util.BHttpResUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/8/23.
 */
@RestController()
@RequestMapping("/lesson")
public class LessonController {

    private static Logger logger = LoggerFactory.getLogger(LessonController.class);

    @Autowired
    LessonService lessonService;
    @Autowired
    ClassInfoService classInfoService;

    @RequestMapping(value = "/get_lesson_exam_subject.do", method = RequestMethod.POST)
    public void getLessonExamSubject(HttpServletRequest request,
                                     HttpServletResponse response) {
        HttpResContent<List<Map<String, Object>>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程考试分科信息");
        try {
            List<Map<String, Object>> list = ExamTypeEnum.getAllExamType(false);
            saveSuccessMsgAndData(hrc, list);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/save_lesson_info.do", method = RequestMethod.POST)
    public void saveLessonInfo(HttpServletRequest request,
                               HttpServletResponse response,
                               LessonInfoBean lessonInfoBean) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("保存课程信息");
        try {
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            LessonInfo lesson = lessonService.getLessonInfoByLessonName(lessonInfoBean.getLessonName());
            if (lesson != null) {
                throw new Exception("课程名重复");
            }
            LessonTypeEnum type = LessonTypeEnum.getEnum(lessonInfoBean.getType());
            if (type == null) {
                throw new Exception("无效的课程类型");
            }
            Date date = new Date();
            lesson = lessonInfoBean.getLessonInfo();
            lesson.setCreateUserId(currentUserId);
            lesson.setIfOnShelf(true);
            lesson.setCreateTime(new Date());
            lessonService.saveLessonInfo(lesson);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_lesson_info.do", method = RequestMethod.POST)
    public void updateLessonInfo(HttpServletRequest request,
                                 HttpServletResponse response,
                                 LessonInfoBean lessonInfoBean) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改课程信息");
        try {
            LessonInfo lesson = lessonService.getLessonInfoByLessonName(lessonInfoBean.getLessonName());
            if (lesson != null && !lesson.getId().equals(lessonInfoBean.getId())) {
                throw new Exception("课程名重复");
            }
            if (lessonInfoBean.getType() != null) {
                LessonTypeEnum type = LessonTypeEnum.getEnum(lessonInfoBean.getType());
                if (type == null) {
                    throw new Exception("无效的课程类型");
                }
            }
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            LessonInfo lessonInfo = lessonInfoBean.getLessonInfo();
            lessonService.updateLessonInfo(lessonInfo, currentUserId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info.do", method = RequestMethod.POST)
    public void getLessonInfo(HttpServletRequest request,
                              HttpServletResponse response,
                              LessonPageBean lessonPageBean) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程信息");
        try {
            List<String> myEmployee = RedisUtils.getInstance().getDataPermissionEmployee(request);
            PageData lessonInfos = lessonService.getByCreateUserIds(lessonPageBean, myEmployee);
            saveSuccessMsgAndData(hrc, lessonInfos);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info_search.do", method = RequestMethod.POST)
    public void getLessonInfoSearch(HttpServletRequest request,
                                    HttpServletResponse response,
                                    LessonPageBean lessonPageBean) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程信息");
        try {
            List<String> myEmployee = RedisUtils.getInstance().getDataPermissionEmployee(request);
            PageData lessonInfos = lessonService.getByCreateUserIdsAndSearch(lessonPageBean, myEmployee);
            saveSuccessMsgAndData(hrc, lessonInfos);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info_by_id_for_update.do", method = RequestMethod.POST)
    public void getLessonInfoByIdForUpdate(HttpServletRequest request,
                                           HttpServletResponse response,
                                           Integer lessonId) {
        HttpResContent<LessonInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改获取课程的详细信息");
        try {
            LessonInfo lessonInfo = lessonService.getLessonInfoById(lessonId);
            if (lessonInfo == null) {
                throw new Exception("无此课程信息");
            }
            List<ExamSubjectBean> examSubjects = new ArrayList<>();
            for (LessonSubjectInfo subInfo : lessonInfo.getSubjectInfos()) {
                examSubjects.add(ExamSubjectBean.getExamSubjectBean(subInfo));
            }
            lessonInfo.setSubjectInfos(null);
            lessonInfo.setExamSubjectBean(examSubjects);
            lessonInfo.setSchoolCampus(StudentCampusEnum.getMap(lessonInfo.getLessonCampus()));
            saveSuccessMsgAndData(hrc, lessonInfo);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_info_by_id.do", method = RequestMethod.POST)
    public void getLessonInfoById(HttpServletRequest request,
                                  HttpServletResponse response,
                                  Integer lessonId) {
        HttpResContent<LessonInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程的详细信息");
        try {
            LessonInfo lessonInfo = lessonService.getLessonInfoById(lessonId);
            if (lessonInfo == null) {
                throw new Exception("无此课程信息");
            }
            List<ExamSubjectBean> examSubjects = new ArrayList<>();
            lessonInfo.setSchoolCampus(StudentCampusEnum.getMap(lessonInfo.getLessonCampus()));
            lessonInfo.setExamName(ExamTypeEnum.getDescription(lessonInfo.getExam()));
            saveSuccessMsgAndData(hrc, lessonInfo);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_lesson_classInfo.do", method = RequestMethod.POST)
    public void getLessonClassInfo(HttpServletRequest request,
                                   HttpServletResponse response,
                                   Integer page,
                                   Integer rows,
                                   Integer lessonId,
                                   String type) {
        HttpResContent<PageData> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取课程的班级信息");
        try {
            ClassStatusEnum userTypeEnum = ClassStatusEnum.getEnum(type);
            if (userTypeEnum == null) {
                throw new Exception("无效的班级状态");
            }
            PageData pageData = classInfoService.getClassInfoByLessonIdAndStatus(page, rows, lessonId, userTypeEnum);
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/upate_lesson_info_ifOnShelf.do", method = RequestMethod.POST)
    public void delLessonInfo(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer lessonId,
                              Boolean ifOnShelf) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("上架或下架课程");
        try {
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            lessonService.updateLessonIfOnShelf(lessonId, ifOnShelf, currentUserId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_lesson_subject_info_by_id.do", method = RequestMethod.POST)
    public void delLessonInfo(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer lessonSubjectId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除课时");
        try {
            String currentUserId = RedisUtils.getInstance().getUserId(request);
            lessonService.deleteSubject(lessonSubjectId, currentUserId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_student_unDivide_by_lesson_id.do", method = RequestMethod.POST)
    public void getStudent(HttpServletRequest request,
                           HttpServletResponse response,
                           Integer lessonId) {
        HttpResContent<List<StudentCourseInformationAdapter>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取未分班的学生");
        try {
            saveSuccessMsgAndData(hrc, classInfoService.getUnDivideStudentByLessonId(lessonId));
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/divide_student.do", method = RequestMethod.POST)
    public void divideStudent(HttpServletRequest request,
                              HttpServletResponse response,
                              Integer courseId,
                              Integer classId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("给学生分班");
        try {
            String userId = RedisUtils.getInstance().getUserId(request);
            classInfoService.divideStudent(courseId, classId, userId);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }

}
